* set path

	global raw "~/agriculture_temperature"
	
	global temp "~/revision_codes/temp"
	global visual "~/revision_codes/visual"
	
	use $raw/data/merged_for_reg, clear	
	
	xtset ctycode year

* set reference bin

	rename dd5bin10 REF_BIN
	rename ma5dd5bin10 ma5REF_BIN
	
********************************************************************************	
*******************moving average model：Figure 7*******************************
********************************************************************************	
	* SR + IR effects based on MA
	
	tempfile r1 r2
	
		foreach x in farming_output livestock_output forest_output fishery_output{
			foreach t in 5 {
				qui reghdfe ln_`x' dd5bin0 dd5bin5 dd5binNeg dd5bin15 dd5bin20 dd5bin25 dd5bin30 dd5bin35 ///
				c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
					ma`t'dd5bin0 ma`t'dd5bin5 ma`t'dd5binNeg ma`t'dd5bin15 ma`t'dd5bin20 ma`t'dd5bin25 ma`t'dd5bin30 ma`t'dd5bin35 ///
					c.ma`t'pre##c.ma`t'pre c.ma`t'ssd##c.ma`t'ssd c.ma`t'rhu##c.ma`t'rhu c.ma`t'prs##c.ma`t'prs c.ma`t'win##c.ma`t'win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & region3==1, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
				estimates store n_`x'_ma`t'
		parmest , label saving(`"`r1'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")
		

		*esttab n_*_ma*, b(4) se(4) drop(*year* _cons) brackets	r2 star(* 0.1 ** 0.05 *** 0.01)
		
				qui reghdfe ln_`x' dd5bin0 dd5bin5 dd5binNeg dd5bin15 dd5bin20 dd5bin25 dd5bin30 dd5bin35 ///
				c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
					ma`t'dd5bin0 ma`t'dd5bin5 ma`t'dd5binNeg ma`t'dd5bin15 ma`t'dd5bin20 ma`t'dd5bin25 ma`t'dd5bin30 ma`t'dd5bin35 ///
					c.ma`t'pre##c.ma`t'pre c.ma`t'ssd##c.ma`t'ssd c.ma`t'rhu##c.ma`t'rhu c.ma`t'prs##c.ma`t'prs c.ma`t'win##c.ma`t'win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & region3==2, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
				estimates store s_`x'_ma`t'
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")
		
			
			preserve 
			use `"`r1'"',clear
			append using `"`r2'"'
			gen sector = "`x'"
			rename id  spec
			save $temp/est_ma5_`x',replace
			restore			
			
			}
		}		

		*esttab s_*_ma*, b(4) se(4) drop(*year* _cons) brackets	r2 star(* 0.1 ** 0.05 *** 0.01)		

		
	********************************************************
	*************** arrange the coefficient ****************
	********************************************************
	clear
	foreach file in farming_output forest_output livestock_output fishery_output {
		append using $temp/est_ma5_`file'
	}
	
	// keep temp variables only
	keep if strmatch(parm,"*dd5bin*")
	
	//level varibables
	gen level = -2.5 if strmatch(parm, "*dd5binNeg")
	replace level = 2.5 if strmatch(parm, "*dd5bin0")
	replace level = 7.5 if strmatch(parm, "*dd5bin5")
	*replace level = 12.5 if strmatch(parm, "*dd5bin10")
	replace level = 17.5 if strmatch(parm, "*dd5bin15")
	replace level = 22.5 if strmatch(parm, "*dd5bin20")
	replace level = 27.5 if strmatch(parm, "*dd5bin25")
	replace level = 32.5 if strmatch(parm, "*dd5bin30")
	replace level = 37.5 if strmatch(parm, "*dd5bin35")

	gen type = "intermediate-run" if strmatch(parm, "*ma*")
	replace type = "short-run" if type == ""
	
	// sector name 
	replace sector = "Cropping" if strmatch(sector,"farming_output")
	replace sector = "Livestock" if strmatch(sector,"livestock_output")
	replace sector = "Forestry" if strmatch(sector,"forest_output")
	replace sector = "Fishery" if strmatch(sector,"fishery_output")
		
	rename estimate coef 
	rename stderr se 
	rename min95 ci_l
	rename max95 ci_h
	
	// add zero-value lines
	bysort spec sector type : gen last = _n == _N
	expand 2 if last
	sort spec sector type parm
	replace level = 12.5 if parm==parm[_n-1]
	replace coef = 0 if parm==parm[_n-1]
	replace se = 0 if parm==parm[_n-1]
	replace ci_h = 0 if parm==parm[_n-1]
	replace ci_l = 0 if parm==parm[_n-1]	
	
	
	save $visual/visual_ma5,replace
	
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}
